package com.ui.basic.controller.admin;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.basic.base.BaseController;
import com.common.basic.util.Globals;
import com.ui.basic.command.CommandLoc;
import com.ui.basic.service.admin.LocationService;

@Controller
public class SetupLocationController extends BaseController {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(SetupLocationController.class);
	
	@Autowired
	private LocationService locationService;

	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}
	
	@RequestMapping(value = "/admin/location.page", method = RequestMethod.GET)
	public String page(
			final HttpServletRequest request,
			final HttpServletResponse response) {
		LOGGER.debug(">>>SetupLocationController.page()");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		return "location.page";
	}

	@RequestMapping(value = "/admin/location.view", method = RequestMethod.POST)
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandLoc") CommandLoc commandLoc) {
		LOGGER.debug(">>>SetupLocationController.view");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			locationService.setComboData(commandLoc);
			model.addAttribute("commandLoc", locationService.getLocation(commandLoc));
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}
		return "location.view";
	}

	@RequestMapping(value = "/admin/location.form", method = RequestMethod.POST)
	public String edit1(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandLoc") CommandLoc commandLoc) {
		LOGGER.debug(">>>SetupLocationController.edit1");
		return edit(model, request, response, commandLoc);
	}
	
	@RequestMapping(value = "/admin/location.edit", method = RequestMethod.POST)
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandLoc") CommandLoc commandLoc) {
		LOGGER.debug(">>>SetupLocationController.edit");

		// security validation
		if (!(Boolean) SecurityUtils.getSubject().getSession().getAttribute(Globals.ERROR_REQUESTED_LINK)) {
			LOGGER.error("ERROR - security validation");
			SecurityUtils.getSubject().getSession().removeAttribute(Globals.ERROR_REQUESTED_LINK);
			sendError(response, getMessage(request, "error.permissionDenied",
					new String[] { (String) SecurityUtils.getSubject()
							.getSession().getAttribute(
									Globals.CURRENT_USER_NAME) }));
			return null;
		}


		try {
			locationService.setComboData(commandLoc);

			if (commandLoc.getId() != null)
				model.addAttribute("commandLoc", locationService.getLocationDetail(commandLoc));
			else {
				model.addAttribute("commandLoc", commandLoc);
				commandLoc.setStrAction(Globals.ACTION_ADD);
			}
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}

		return "location.edit";
	}	
	
	@RequestMapping(value = "/admin/location.save", method = RequestMethod.POST)
	public String save(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandLoc") CommandLoc commandLoc,
			BindingResult result, Model model) {
		LOGGER.debug(">>>SetupLocationController.save");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		validator.validate(commandLoc, result);

		if (!hasFieldErrors(request, response, result)) {
			try {
				locationService.save(commandLoc);
				return view(model, request, response, commandLoc);
			} catch (Exception e) {
				if (commandLoc.getId() != null)
					sendError(response, getMessage(request,
							"error.locationdata.saveFailed",
							new String[] { commandLoc.getLocName()}));
				else
					sendError(response, getMessage(request,
							"error.locationdata.exist", new String[] { commandLoc.getLocName()}));
				return null;
			}
		}
		return null;
	}

	@RequestMapping(value = "/admin/location.del", method = RequestMethod.POST)
	public String delete(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandLoc") CommandLoc commandLoc,
			BindingResult result) {
		LOGGER.debug(">>>SetupLocationController.delete");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		if (commandLoc.getId() != null
				|| (commandLoc.getSelectedRecord() != null && commandLoc
						.getSelectedRecord().length > 0)) {
			try {
				locationService.delete(commandLoc);
				return view(model, request, response, commandLoc);
			} catch (Exception e) {
				sendError(response, e.getMessage());
				return null;
			}
		}
		sendError(response, getMessage(request, "label.noDataSelected"));
		return null;
	}
}
